<template>
  <div class="form-top">
    <el-form class="h-flex" action="" style="flex-wrap: wrap;justify-content: flex-start;">
      <p v-if="show.selectBum" class="form-top-p">
        <select-tree
          v-model="valuek.selectBum"
          title="选择部门"
          placeholder="选择部门"
          :options="formz.selectBum"
          :props="defaultProps"
          node-click
          @clear="deptId='';line()"
          @nodeClick="queryDeptId" />
      </p>
      <p v-if="show.selectBmCode" class="form-top-p">
        <select-tree
          v-model="valuek.selectBum"
          title="选择部门"
          placeholder="选择部门"
          :options="formz.selectBum"
          :props="codeProps"
          node-click
          @clear="deptId='';line()"
          @nodeClick="queryDeptId" />
      </p>
      <p v-if="show.selectcej" class="form-top-p">
        <el-select v-model="valuek.selectcej" class="phm-form" placeholder="选择部门" clearable>
          <el-option
            v-for="item in formz.selectcej"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode" />
        </el-select>
        <!--<el-cascader v-model="valuek.selectcej"
                     class="phm-form"
                     placeholder="选择车间"
                     :options="formz.selectcej" />-->
      </p>
      <p v-if="show.selectcej2" class="form-top-p">
        <el-select v-model="valuek.selectcej2" class="phm-form" placeholder="选择车间" clearable @change="deptId=valuek.selectcej2;line()">
          <el-option
            v-for="item in formz.workshopList"
            :key="item.id"
            :label="item.deptName"
            :value="item.id" />
        </el-select>
      </p>
      <p v-if="show.selectLin" class="form-top-p">
        <el-select v-model="valuek.selectLin" class="phm-form" placeholder="选择线路" style="width: 140px;" clearable @change="listByLineId()">
          <el-option
            v-for="item in formz.selectLin"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectQujian" class="form-top-p">
        <el-select v-model="valuek.selectQujian" class="phm-form" placeholder="选择区间站场" clearable>
          <el-option
            v-for="item in formz.selectQujian"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectQuex" class="form-top-p">
        <select-table
          ref="parentSelect"
          v-model="valuek.selectQuex"
          :filter-form="selectTableModel.filterForm"
          :filter-data="selectTableModel.filterData"
          :default-data="parents"
          :columns="selectTableModel.columns"
          :total="selectTableModel.total"
          :filter-fn="selectTableModel.filterFn"
          placeholder="选择缺陷分类"
          @current-change="chooseType"
          @clear="clearType">
          <template slot="devClassName" slot-scope="{row}">
            {{ row.devClassAttr ? row.devClassAttr.devClassName : '' }}
          </template>
        </select-table>
      </p>
      <p v-if="show.parameterClass" class="form-top-p">
        <el-select v-model="valuek.parameterClass" class="phm-form" placeholder="缺陷类别" clearable style="width: 120px;">
          <el-option
            v-for="item in formz.parameterClass"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectLaiy" class="form-top-p">
        <el-select v-model="valuek.selectLaiy" class="phm-form" placeholder="缺陷来源" clearable style="width: 100px;">
          <el-option
            v-for="item in formz.selectLaiy"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.gradeCode" class="form-top-p">
        <el-select v-model="valuek.gradeCode" class="phm-form" placeholder="缺陷等级" clearable style="width: 100px;">
          <el-option
            v-for="item in formz.gradeCode"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectLeix" class="form-top-p">
        <el-select v-model="valuek.selectLeix" class="phm-form" multiple collapse-tags clearable placeholder="选择设备类型" style="width: 150px;" @change="devClassChange">
          <el-option
            v-for="item in formz.selectLeix"
            :key="item.devClassCode"
            :label="item.devClassShortName"
            :value="item.devClassCode" />
        </el-select>
      </p>
      <p v-if="show.selectgzLeix" class="form-top-p">
        <el-select v-model="valuek.selectgzLeix" class="phm-form" placeholder="选择故障类型" clearable @change="findSelectgzyy()">
          <el-option
            v-for="item in formz.selectgzLeix"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectgzyuany" class="form-top-p">
        <el-select v-model="valuek.selectgzyuany" class="phm-form" placeholder="选择故障原因" clearable>
          <el-option
            v-for="item in formz.selectgzyuany"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectgzxz" class="form-top-p">
        <el-select v-model="valuek.selectgzxz" class="phm-form" placeholder="选择故障性质" clearable>
          <el-option
            v-for="item in formz.selectgzxz"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.faultLevel && valuek.selectgzxz === 1" class="form-top-p">
        <el-select v-model="valuek.faultLevel" class="phm-form" placeholder="选择事故等级" clearable>
          <el-option label="选择事故等级" value="" />
          <el-option label="特别重大事故" value="1" />
          <el-option label="重大事故" value="2" />
          <el-option label="较大事故" value="3" />
          <el-option label="一般事故" value="4" />
        </el-select>
      </p>
      <p v-if="show.selectXingb" class="form-top-p">
        <el-select v-model="valuek.selectXingb" class="phm-form" placeholder="行别" clearable>
          <el-option
            v-for="item in formz.selectXingb"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectSpecific" class="form-top-p">
        <el-select v-model="valuek.specificDefectsId" class="phm-form" placeholder="特殊缺陷" clearable style="width: 100px;" @change="setSpecParams">
          <el-option
            v-for="item in formz.selectSpecific"
            :key="item.id"
            :label="item.specificDefectsName"
            :value="item.id" />
        </el-select>
      </p>
      <p v-if="show.selectReason" class="form-top-p">
        <el-select v-model="valuek.reasonType" class="phm-form" placeholder="缺陷成因" clearable style="width: 100px;margin-right: 10px;">
          <el-option label="内部原因" value="1" />
          <el-option label="外部原因" value="2" />
        </el-select>
        <el-select v-model="valuek.fatal" class="phm-form" placeholder="是否致命" clearable style="width: 120px;">
          <el-option label="致命缺陷" value="1" />
          <el-option label="非致命缺陷" value="0" />
        </el-select>
      </p>
      <p v-if="show.selectZuangt" class="form-top-p">
        <el-select v-model="selectZuangtArr" class="phm-form" placeholder="处理状态" multiple collapse-tags clearable style="width: 140px;" @change="setZuangt">
          <el-option
            v-for="item in formz.selectZuangt"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectJih" class="form-top-p">
        <el-select v-model="valuek.selectJih" class="phm-form" placeholder="计划检修" clearable>
          <el-option
            v-for="item in formz.selectJih"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectYuj" class="form-top-p">
        <el-select v-model="valuek.selectYuj" class="phm-form" placeholder="预警级别" clearable>
          <el-option
            v-for="item in formz.selectYuj"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectYichang" class="form-top-p">
        <el-select v-model="valuek.selectYichang" class="phm-form" multiple placeholder="参数异常类型" collapse-tags clearable style="width: 200px;" @change="setAttCode">
          <el-option
            v-for="item in formz.selectYichang"
            :key="item.defectTypeCode"
            :label="item.defectTypeName"
            :value="item.defectTypeCode" />
        </el-select>
      </p>
      <p v-if="show.deviceStatus" class="form-top-p">
        <el-select v-model="valuek.statusType" class="phm-form" placeholder="设备状态类型" style="margin-bottom: 10px" clearable @change="changeStatusType">
          <el-option label="部件状态" value="1" />
          <el-option label="部件安装状态" value="2" />
          <el-option label="零件状态" value="3" />
          <el-option label="零件安装状态" value="4" />
        </el-select>
      </p>
      <p v-if="show.deviceStatus && valuek.statusType !== ''" class="form-top-p">
        <el-select v-model="valuek.deviceStatusArr" class="phm-form" multiple placeholder="设备状态描述" collapse-tags clearable style="width: 130px;" @change="setStatusCode">
          <el-option
            v-for="item in formz.deviceStatusVisible"
            :key="item.key"
            :label="item.label"
            :value="item.key" />
        </el-select>
      </p>
      <p v-if="show.selectZhuangt" class="form-top-p">
        <el-select v-model="valuek.selectZhuangt" class="phm-form" placeholder="处理状态" clearable>
          <el-option
            v-for="item in formz.selectZhuangt"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.selectJxzt" class="form-top-p">
        <el-select v-model="valuek.selectJxzt" class="phm-form" placeholder="检修状态" clearable>
          <el-option
            v-for="item in formz.selectJxzt"
            :key="item.value"
            :label="item.label"
            :value="item.value" />
        </el-select>
      </p>
      <p v-if="show.inputSheb" class="form-top-p">
        <el-input v-model="valuek.inputSheb" placeholder="设备名称" />
      </p>
      <p v-if="show.inputJih" class="form-top-p">
        <el-input v-model="valuek.inputJih" placeholder="计划号" />
      </p>
      <p v-if="show.inputJice" class="form-top-p">
        <el-input v-model="valuek.inputJice" placeholder="机车号" />
      </p>
      <p v-if="show.selectShij" class="form-top-p">
        <el-date-picker
          v-model="valuek.selectShij"
          class="phm-form input-long"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          clearable />
      </p>
      <p v-if="show.selectShijA" class="form-top-p">
        <el-date-picker
          v-model="valuek.selectShij"
          class="phm-form input-long"
          type="daterange"
          range-separator="至"
          start-placeholder="发现日期"
          end-placeholder="发现日期"
          value-format="yyyy-MM-dd"
          clearable />
      </p>
      <p v-if="show.selectShijA" class="form-top-p">
        <el-date-picker
          v-model="valuek.selectShijA"
          class="phm-form input-long"
          type="daterange"
          range-separator="至"
          start-placeholder="处理日期"
          end-placeholder="处理日期"
          value-format="yyyy-MM-dd"
          clearable />
      </p>
      <p v-if="show.pillarCode" class="form-top-p">
        <el-input
          v-model="valuek.pillarCode"
          placeholder="支柱号"
          clearable
          style="width: 110px" />
      </p>
      <p v-if="show.selectShijB" class="form-top-p">
        <el-date-picker
          v-model="valuek.selectShijB"
          type="month"
          placeholder="预测月份"
          clearable
          style="width: 110px" />
      </p>
      <p v-if="show.selectShijC" class="form-top-p">
        <el-date-picker
          v-model="valuek.selectShijB"
          type="month"
          placeholder="选择月份"
          clearable
          style="width: 110px" />
      </p>
      <p class="form-top-p">
        <el-button type="primary" size="mini" class="form-button" icon="el-icon-search" @click="onSubmit()">查询</el-button>
      </p>
      <p class="form-top-p">
        <export-excel v-if="show.buttondaoc" ref="export" size="mini" table-id="dataTable" btn-text="导出" file-name="清单" style="float: left;" @before="getAllList" @complete="onSubmit" />
      </p>

      <a class="form-qieh" @click="isActive=!isActive" />
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import ExportExcel from './ExportExcel/index'
import { getListPage } from '@/api/cms/detect-data/information'
import { line, listByLineId, dept, listDirection, getDefectTypeList, getAllJcwDevClass, getAllJcwDevStatus, deptTree } from '@/api/cms/detect-data/information'
import { getListAllTree } from '@/api/cms/detect-data/information'
import SelectTable from './SelectTable'
import { getListTree as devClassList } from '@/api/cms/detect-data/information'
import { getWorkshopList } from '@/api/cms/detect-data/information'
import { getSpecDefect } from '@/api/cms/detect-data/information'
export default {
  name: 'Searchtop',
  components: {
    SelectTable,
    ExportExcel
  },
  props: {
    width: {
      type: String,
      default: '300px'
    },
    showOption: {
      type: Array,
      default: () => []
    },
    currentQuery: {
      type: Object,
      default: () => {
        return {}
      }
    },
    addPillar: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 数据默认字段
      defaultProps: {
        parent: 'parentId', // 父级唯一标识
        value: 'id', // 唯一标识
        label: 'label', // 标签显示
        children: 'children' // 子级
      },
      // 部门树－编码
      codeProps: {
        parent: 'parentId', // 父级唯一标识
        value: 'deptCode', // 唯一标识
        label: 'deptName', // 标签显示
        children: 'children' // 子级
      },
      parents: [],
      defectTypeTotal: 0,
      selectTableModel: {
        filterForm: {
          page: 1,
          size: 10,
          devClassId: ''
        },
        filterData: {
          devClassId: {
            key: 'devClassId',
            type: 'selectTree',
            title: '设备分类',
            placeholder: '所有设备分类',
            options: [],
            props: {
              label: 'label',
              value: 'id',
              parent: 'parentId',
              children: 'children'
            },
            changeFn: _ => {
              this.$refs.parentSelect.$refs.customSelect.focus()
            }
          },
          keyName: {
            key: 'keyName',
            type: 'input',
            placeholder: '请输入名称、编码'
          }
        },
        columns: [
          { property: 'devClassName', label: '设备分类', formatter: row => row.devClassAttr ? row.devClassAttr.devClassName : '' },
          { property: 'userDefectTypeName', label: '分类名称' },
          { property: 'parentName', label: '上级分类' }
        ],
        total: 0,
        filterFn: getListPage
      },
      firstChange: true, // 是否带参数信息第一次进入
      deviceStatus: [], // 所有设备状态列表
      deptId: '', // 部门id 用于查询部门关联的线路、区间站场
      valuek: {
        selectZhuangt: [],
        selectLin: [], // 选择线路
        selectQujian: [], // 选择区间站场
        selectGongq: [], // 工区
        selectXingb: [], // 行别
        selectZuangt: [], // 状态
        selectShij: '', // 时间
        selectShijA: '', // 时间1
        selectShijB: '', // 预测月份
        inputSheb: '', // 设备名称
        selectJih: [], // 计划检修
        selectQuex: '', // 缺陷分类
        fatal: '', // 是否致命
        reasonType: '', // 内因外因
        selectBum: '', // 部门
        selectLeix: [], // 设备类型数组
        devClassCode: '', // 设备类型
        deviceStatusArr: [], // 设备状态数组
        statusCode: '', // 设备状态编码
        statusType: '', // 设备状态类型
        selectcej: [], // 选择车间
        selectcej2: [], // 选择车间
        selectLaiy: [], // 缺陷来源
        gradeCode: '', // 缺陷来源
        selectYuj: [], // 预警级别
        selectYichang: [], // 异常类型数组
        attCode: '', // 异常类型
        proCode: '', // 设备项目代码
        parameterClass: '', // 参数或零部件缺陷类别
        selectJxzt: [],
        inputJice: '',
        inputJih: '',
        selectgzLeix: '',
        selectgzyuany: '',
        selectgzxz: [],
        faultLevel: [],
        defectTypeId: '', // 缺陷分类id
        defectTypeName: '', // 缺陷分类name
        specificDefectsCode: '', // 特殊缺陷
        pillarCode: '', // 支柱号
        specificDefectsId: '' // 特殊缺陷
      },
      show: {
        parameterClass: '', // 参数或零部件缺陷类别
        selectLin: false, // 选择线路
        selectQujian: false, // 选择区间站场
        selectGongq: false, // 工区
        selectXingb: false, // 行别
        selectZuangt: false, // 状态
        selectShij: false, // 时间
        selectShijA: false, // 时间1
        selectShijB: false, // 预测月份
        selectShijC: false, // 选择月份
        inputSheb: false, // 设备名称
        selectJih: false, // 计划检修
        selectQuex: false, // 缺陷分类
        selectReason: false, // 内因外因，致命非致命
        selectBum: false, // 部门－id
        selectBmCode: false, // 部门树－编码
        buttondaoc: false, // 导出
        selectcej: false, // 选择车间
        selectcej2: false, // 选择车间
        selectLeix: false, // 选择设备类型
        deviceStatus: false, // 设备装备状态
        selectLaiy: false, // 缺陷来源
        gradeCode: false, // 缺陷级别
        selectYuj: false, // 选择预警级别
        selectYichang: false, // 异常类型
        inputJice: false, // 机车号
        inputJih: false, // 计划号
        selectJxzt: false, // 检修状态
        selectgzLeix: false, // 故障类型
        selectgzyuany: false, // 故障原因
        selectgzxz: false, // 故障性质
        faultLevel: false, // 故障等级
        selectZhuangt: false, // 处理状态
        pillarCode: false, // 支柱号
        selectSpecific: false // 特殊缺陷

      },
      selectZuangtArr: [], // 处理状态
      formz: {
        selectZhuangt: [
          {
            value: '1',
            label: '已处理'
          }, {
            value: '2',
            label: '未处理'
          }
        ],
        selectJxzt: [
          {
            value: 'chaoqi',
            label: '超期'
          }, {
            value: 'linqi',
            label: '提醒'
          }

        ],
        parameterClass: [
          {
            value: 1,
            label: '参数异常缺陷'
          },
          {
            value: 2,
            label: '零部件缺陷'
          },
          {
            value: 3,
            label: '其他'
          }
        ], // 参数或零部件缺陷类别
        selectLin: [],
        selectQujian: [],
        selectcej: [], // 选择车间
        selectGongq: [], // 工区
        selectBum: [], // 部门
        selectQuex: [], // 缺陷分类
        selectXingb: [], // 行别
        selectZuangt: [
          {
            value: '0',
            label: '未处理'
          }, {
            value: '1',
            label: '已审核'
          }, {
            value: '2',
            label: '下发'
          }, {
            value: '3',
            label: '已整改'
          }, {
            value: '4',
            label: '已销号'
          }
        ], // 状态
        selectLaiy: [
          {
            value: '1',
            label: '检修'
          }, {
            value: '2',
            label: '巡视'
          }, {
            value: '3',
            label: '检测'
          }, {
            value: '5',
            label: '其它'
          }
        ], // 缺陷来源
        gradeCode: [
          {
            value: '1',
            label: '一级缺陷'
          }, {
            value: '2',
            label: '二级缺陷'
          }
        ], // 缺陷级别
        selectJih: [], // 计划检修
        selectLeix: [], // 选择设备类型
        selectYuj: [], // 预警级别
        selectYichang: [], // 异常类型
        deviceStatusVisible: [], // 设备状态数组
        selectgzLeix: [{
          value: 0,
          label: '非跳闸'
        }, {
          value: 1,
          label: '跳闸'
        }],
        selectgzyuany: [],
        selectgzxz: [{
          value: 0,
          label: '非事故'
        }, {
          value: 1,
          label: '事故'
        }],
        workshopList: [], // 车间集合
        selectSpecific: [] // 特殊缺陷
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  mounted() {
    this.showOption.forEach(item => {
      this.show[item] = true
    })
    // 查询条件回显
    if (this.currentQuery.selectShij) {
      this.valuek.selectShij = this.currentQuery.selectShij
    }
    if (this.currentQuery.faultType === 0 || this.currentQuery.faultType === 1) {
      this.valuek.selectgzLeix = this.currentQuery.faultType
      this.findSelectgzyy()
    }
    if (this.currentQuery.gradeCode) {
      // console.log(this.show.faultLevel)
      if (this.show.faultLevel) {
        this.valuek.faultLevel = this.currentQuery.gradeCode
      } else {
        this.valuek.gradeCode = this.currentQuery.gradeCode
      }
    }
    if (this.currentQuery.statusType) {
      this.valuek.statusType = this.currentQuery.statusType
      if (this.currentQuery.statusCode) {
        this.valuek.statusCode = this.currentQuery.statusCode
        this.valuek.deviceStatusArr = this.currentQuery.statusCode.split(',')
      }
    }
    if (this.currentQuery.typeClass) {
      this.valuek.selectLaiy = this.currentQuery.typeClass
    }
    if (this.currentQuery.faultQualitative === 0 || this.currentQuery.faultQualitative === 1) {
      this.valuek.selectgzxz = this.currentQuery.faultQualitative
    }
    if (this.currentQuery.overhaulStatus) {
      this.valuek.selectJxzt = this.currentQuery.overhaulStatus
    }
    if (this.currentQuery.reasonType) {
      this.valuek.reasonType = this.currentQuery.reasonType
    }
    if (this.currentQuery.fatal) {
      this.valuek.fatal = this.currentQuery.fatal
    }
    if (this.currentQuery.parameterClass) {
      this.valuek.parameterClass = this.currentQuery.parameterClass
    }
    if (this.currentQuery.devClassCode) {
      this.valuek.devClassCode = this.currentQuery.devClassCode
      this.valuek.selectLeix = this.currentQuery.devClassCode.split(',')
    }
    if (this.currentQuery.attCode) {
      this.valuek.attCode = this.currentQuery.attCode
      this.valuek.selectYichang = this.currentQuery.attCode.split(',')
    }
    if (this.currentQuery.deptId) {
      this.deptId = this.currentQuery.deptId
    }
    if (this.currentQuery.selectZuangt) {
      this.valuek.selectZuangt = this.currentQuery.selectZuangt
      this.selectZuangtArr = this.currentQuery.selectZuangt.split(',')
    }
    this.show.selectLin && this.line()
    this.show.selectcej && this.dept()
    this.show.selectXingb && this.listDirection()
    this.show.selectBum && this.getListAllTree()
    this.show.selectBmCode && this.getDeptTree()
    this.show.selectSpecific && this.getSpecDefect()
    this.show.selectQuex && (devClassList().then(response => {
      this.selectTableModel.filterData.devClassId.options = response.data
    }), this.getDefectTypePage())
    this.show.selectcej2 && getWorkshopList({ deptTypeCode: '22' }).then(response => {
      // 获取车间列表
      this.formz.workshopList = response.data
      response.data.forEach(item => {
        if (this.currentQuery.deptCode && item.deptCode === this.currentQuery.deptCode) {
          this.valuek.selectcej2 = item.id
          this.deptId = item.id
        }
      })
      this.show.selectLin && this.line()
    })
    this.show.selectYichang && getDefectTypeList().then(response => {
      // console.log('参数异常类型', response)
      const attList = []
      response.data && response.data.forEach(item => {
        attList.push({
          defectTypeCode: item.attCode,
          defectTypeName: item.defectTypeName
        })
      })
      this.formz.selectYichang = attList
    })
    this.show.selectLeix && getAllJcwDevClass().then(response => {
      // 获取所有设备类型
      this.formz.selectLeix = []
      if (response.data) {
        if (this.addPillar) {
          this.formz.selectLeix = [{
            devClassCode: '14',
            devClassName: '支柱',
            devClassShortName: '支柱',
            id: ''
          }, ...response.data]
        } else {
          this.formz.selectLeix = response.data
        }
      }
    })
    this.show.deviceStatus && getAllJcwDevStatus().then(response => {
      const statusList = []
      response.data && response.data.forEach(item => {
        statusList.push({
          key: item.statusCode,
          label: item.statusName,
          type: item.statusType
        })
      })
      this.deviceStatus = statusList
      if (this.currentQuery.statusType) {
        this.changeStatusType()
      }
      // this.formz.deviceStatusVisible = statusList
    })
  },
  methods: {
    onSubmit() {
      this.$emit('search', this.valuek)
    },
    getAllList() {
      this.$emit('search', { ...this.valuek, size: 99999 })
    },
    exportExcel() {
      setTimeout(_ => {
        this.$refs.export.exportExcel()
      }, 500)
    },
    queryDeptId(tree, node) {
      // console.log(node.id)
      this.deptId = node.id
      this.line()
    },
    line() {
      this.valuek.selectLin = ''
      this.formz.selectLin = []
      this.valuek.selectQujian = ''
      this.formz.selectQujian = []
      line({ deptId: this.deptId }).then(response => {
        // console.log('线路下拉框', response)
        const lines = []
        if (response.data) {
          response.data.forEach(item => {
            const line = {}
            line.value = item.lineId + '-' + item.lineCode
            line.label = item.lineName
            lines.push(line)
            if ((this.currentQuery.lineId && item.lineId === this.currentQuery.lineId) || (this.currentQuery.lineCode && item.lineCode === this.currentQuery.lineCode)) {
              this.valuek.selectLin = item.lineId + '-' + item.lineCode
              this.listByLineId()
            }
          })
        }
        this.formz.selectLin = lines
      })
    },
    listByLineId() {
      const deptId = this.deptId
      const line = this.valuek.selectLin
      this.valuek.selectQujian = ''
      this.formz.selectQujian = []
      if (line) {
        const lineCode = line.split('-')[0]
        listByLineId({ deptId: deptId, lineId: lineCode }).then(response => {
          // console.log('区间下拉框', response)
          const sites = []
          if (response.data) {
            response.data.forEach(item => {
              const site = {}
              site.value = item.siteId + '-' + item.lineSiteCode
              site.label = item.siteName
              sites.push(site)
              if ((this.currentQuery.siteId && item.siteId === this.currentQuery.siteId && this.firstChange) || (this.currentQuery.lineSiteCode && item.lineSiteCode === this.currentQuery.lineSiteCode && this.firstChange)) {
                this.firstChange = false
                this.valuek.selectQujian = item.siteId + '-' + item.lineSiteCode
              }
            })
          }
          this.formz.selectQujian = sites
        })
      }
    },
    dept() {
      dept().then(response => {
        // console.log('部门下拉框', response)
        if (response.data) {
          const depts = response.data
          // this.changeName(depts)
          this.formz.selectcej = depts
          if (this.currentQuery.deptCode) {
            this.valuek.selectcej = this.currentQuery.deptCode
          }
        }
      })
    },
    getListAllTree() {
      // 获取部门树－id
      getListAllTree().then(response => {
        // console.log('部门树', response)
        if (response.data) {
          this.formz.selectBum = response.data
          if (this.currentQuery.deptId) {
            this.valuek.selectBum = this.currentQuery.deptId
          }
        }
      })
    },
    getDeptTree() {
      // 获取部门树－code
      let param = {}
      if (this.userInfo.userDeptAttr && this.userInfo.userDeptAttr.segmentFlag) {
        // 目前userInfo中没有deptCode属性
        param = { parentDeptCode: this.currentQuery.sectionCode }
      }
      deptTree(param).then(response => {
        // console.log('部门树', response)
        if (response.data) {
          this.formz.selectBum = response.data
          if (this.currentQuery.deptCode) {
            this.valuek.selectBum = this.currentQuery.deptCode
          }
        }
      })
    },
    remapDeptTree(tree) {
      // 添加部门code
      tree.map(item => {
        item.deptCode = item.obj.deptCode
        if (item.children && item.children.length > 0) {
          item.children = this.remapDeptTree(item.children)
        }
      })
      return tree
    },
    getSpecDefect() {
      // 获取特定缺陷
      const param = {
        // parameterClass: '',
        // devClassCode: this.valuek.devClassCode,
        // statusType: this.valuek.statusType,
        // statusCode: this.valuek.statusCode
      }
      this.formz.selectSpecific = []
      this.valuek.specificDefectsId = ''
      getSpecDefect(param).then(response => {
        // console.log(response.data)
        if (response.data && response.data.length > 0) {
          this.formz.selectSpecific = response.data
          this.formz.selectSpecific.forEach(item => {
            if (this.currentQuery.specCode === item.specificDefectsCode) {
              this.valuek.specificDefectsId = item.id
              this.setSpecParams()
            }
          })
          // this.$emit('search', this.valuek)
        }
      })
    },
    setSpecParams() {
      // 设置特定缺陷相关参数
      // proCode
      // parameterClass
      this.valuek.statusCode = ''
      this.valuek.statusType = ''
      this.valuek.devClassCode = ''
      this.valuek.proCode = ''
      this.valuek.parameterClass = ''
      this.formz.selectSpecific.forEach(item => {
        if (this.valuek.specificDefectsId === item.id) {
          this.valuek.proCode = item.proCode
          this.valuek.parameterClass = item.parameterClass
          this.valuek.specificDefectsCode = item.specificDefectsCode
          this.valuek.statusCode = item.statusCode
          this.valuek.statusType = item.statusType
          this.valuek.devClassCode = item.devClassCode
        }
      })
    },
    setZuangt() {
      // 设置特定缺陷相关参数
      // proCode
      // parameterClass
      this.valuek.selectZuangt = this.selectZuangtArr.join(',')
    },
    changeName(a) {
      if (a && a.length) {
        for (let i = 0; i < a.length; i++) {
          if (a[i].deptName) {
            a[i].label = a[i].deptName
            delete a[i].deptName
          }
          if (a[i].deptCode) {
            a[i].value = a[i].deptCode
            delete a[i].deptCode
          }
          if (a[i].children && a[i].children.length) {
            this.changeName(a[i].children)
          } else {
            delete a[i].children
          }
        }
      }
    },
    findSelectgzyy() {
      const selectgzyy = this.valuek.selectgzLeix
      this.valuek.selectgzyuany = ''
      const gzyy = []
      if (selectgzyy === 1) {
        gzyy.push({ value: '1-1', label: '供电原因' })
        gzyy.push({ value: '1-2', label: '机动车原因' })
        gzyy.push({ value: '1-3', label: '外部原因' })
        gzyy.push({ value: '1-4', label: '其他原因' })
      } else if (selectgzyy === 0) {
        gzyy.push({ value: '0-1', label: '变电设备故障' })
        gzyy.push({ value: '0-2', label: '机车故障' })
        gzyy.push({ value: '0-3', label: '机车或者动车组故障' })
        gzyy.push({ value: '0-4', label: '外部原因' })
        gzyy.push({ value: '0-5', label: '异物侵限' })
        gzyy.push({ value: '0-6', label: '自然灾害' })
      }
      this.formz.selectgzyuany = gzyy
      if (this.currentQuery.reasonCode) {
        this.valuek.selectgzyuany = Number(this.currentQuery.reasonCode.split('-')[1])
      }
    },
    listDirection() {
      listDirection().then(response => {
        // console.log('行别下拉框', response)
        const directions = []
        if (response.data) {
          response.data.forEach(item => {
            const direction = {}
            direction.value = item.directionCode
            direction.label = item.directionName
            directions.push(direction)
          })
        }
        this.formz.selectXingb = directions
      })
    },
    getDefectTypePage() {
      getListPage(this.listQuery)
        .then(response => {
          if (!this.parents.length) {
            this.parents = response.data.records.slice(0, 10)
            this.selectTableModel.total = response.data.total
          }
        })
        .catch(_ => {
        })
    },
    setAttCode() {
      // 参数异常类型
      this.valuek.attCode = this.valuek.selectYichang.join(',')
    },
    setStatusCode() {
      // 设备状态编码
      this.valuek.statusCode = this.valuek.deviceStatusArr.join(',')
      this.show.selectSpecific && this.getSpecDefect()
    },
    changeStatusType() {
      // 设备状态类型
      this.formz.deviceStatusArr = []
      this.valuek.statusCode = ''
      const arr = this.deviceStatus.filter(item => {
        return this.valuek.statusType === '' || item.type === parseInt(this.valuek.statusType)
      })
      this.formz.deviceStatusVisible = arr
      this.valuek.deviceStatusArr = this.formz.deviceStatusVisible.map(item => {
        return item.key
      })
      this.valuek.statusType !== '' && this.setStatusCode()
      this.show.selectSpecific && this.getSpecDefect()
    },
    devClassChange() {
      // 设备类型
      this.valuek.devClassCode = this.valuek.selectLeix.join(',')
      this.show.selectSpecific && this.getSpecDefect()
    },
    chooseType(data) {
      this.valuek = Object.assign({}, this.valuek, {
        selectQuex: data.userDefectTypeName,
        defectTypeId: data.id,
        defectTypeName: data.userDefectTypeName
      })
    },
    clearType() {
      this.valuek = Object.assign({}, this.valuek, {
        selectQuex: '',
        defectTypeId: '',
        defectTypeName: ''
      })
    }
  }
}
</script>

